package crawler;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import utility.connection.HttpConnector;
import utility.connection.HttpParameters;
import utility.files.TextFile;

public class LinkFilterRepository {
	
	private ArrayList<String> _links;
	private Pattern _pattern;
	private File _directory;
	private TextFile _summaryFile;

	public LinkFilterRepository(String regex, File file){
		_links = new ArrayList<String>();
		setRegex(regex);
		validate(file);
	}

	public LinkFilterRepository(File file){
		this(".*",file);
	}
	
	private void validate(File file) {
		if(!file.isDirectory()) file = file.getParentFile();
		if(!file.exists()){
			try {
				file.createNewFile();
			} catch (IOException e) {
			}			
		}
		_directory = file;
		_summaryFile = TextFile.inRewrite(new File(_directory.getPath()+"/_links.txt"));		
	}

	
	public void setRegex(String regex){
		_pattern = Pattern.compile(regex);
	}
	
	public void putFor(String link, String server) {
		try {
		Matcher matcher = _pattern.matcher(link);
		if (matcher.matches()) {
			_summaryFile.putLine(link);
			HttpConnector connector = new HttpConnector();
			HttpParameters parameters = new HttpParameters();
			String fileName = fileNameFrom(link);
			TextFile htmlFile;
			htmlFile = TextFile.inRewrite(_directory.getCanonicalPath()
						+ "/" + fileName);
			String result = connector.doGet(link, parameters);
			htmlFile.putLine(result);
			StatisticsFileWriter.getInstance().add(server, 1);
		}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private String fileNameFrom(String link) {
		if(link.startsWith("http://")) link = link.substring(7);
		link = link.replace('/', '_');
		return link;
	}

	public List<String> links() {
		return _links;
	}

}
